import { toStringTypeCheck } from '../../Func/uniFunc/Func/toStringTypeCheck';

/**
 * 剝離 matrStr 的信息
 * @param {Object} matrStr window.getComputedStyle(el).transform
 * @param {Object} type [scalX,skew,rotate,scalY,translateX,translateY]
 */
function spliteMatrix(matrStr, type) {
  if (!toStringTypeCheck(matrStr, 'string')) {
    console.log('sorry the arguments[0] is not String type');
    return false;
  }
  if (matrStr === 'none') {
    return false;
  }
  //matrix(1, 0, 0, 1, 0, -41.6)
  //matrix\((?<scalX>-?\d+(?:\.\d+)?),\s(?<skew>-?\d+(?:\.\d+)?),\s(?<rotate>-?\d+(?:\.\d+)?),\s(?<scalY>-?\d+(?:\.\d+)?),\s(?<translateX>-?\d+(?:\.\d+)?),\s(?<translateY>-?\d+(?:\.\d+)?)\)
  let resultArr = [];
  let regMatrixStr =
    /matrix\((?<scalX>-?\d+(?:\.\d+)?),\s(?<skew>-?\d+(?:\.\d+)?),\s(?<rotate>-?\d+(?:\.\d+)?),\s(?<scalY>-?\d+(?:\.\d+)?),\s(?<translateX>-?\d+(?:\.\d+)?),\s(?<translateY>-?\d+(?:\.\d+)?)\)/;
  let matchResult = matrStr.match(regMatrixStr);
  if (matchResult.groups) {
    for (let key in matchResult.groups) {
      resultArr[key] = matchResult.groups[key];
    }
    // console.log(resultArr);
    if (type != undefined) {
      if (!toStringTypeCheck(type, 'string')) {
        console.log('sorry the arguments[1] is not String type');
        return false;
      }
      return resultArr[type];
    } else {
      return resultArr;
    }
  }
}

export { spliteMatrix };
